package cemp.controller;


import cemp.common.api.reponse.RestStockAllResponse;
import cemp.entity.StockAll;
import cemp.jwt.JwtCommonUser;
import cemp.jwt.JwtTokenUtil;
import cemp.service.FetchDataService;
import cemp.service.StockPlateService;
import cemp.shiro.OAuth2Token;
import com.api.AlarmApi;
import com.api.StockCommonQueryApi;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;


@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/test")
public class TestController {
    private final AlarmApi api;
    private final StockCommonQueryApi stockCommonQueryApi;
    private final FetchDataService fetchDataService;
    private final StockPlateService stockPlateService;
    @GetMapping("/list")
    public String list() {
        System.out.println("success");
        String result = api.list();
        return result;
    }
    @GetMapping("/login")
    public void login( @RequestParam("username")String username, @RequestParam("password")String password) {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        token.setRememberMe(true);
        Subject currentUser = SecurityUtils.getSubject();
        currentUser.login(token);
    }
    @GetMapping("/jwt/login")
    public String jwtLogin(@RequestParam("username")String username, @RequestParam("password")String password) throws Exception {
        JwtCommonUser commonUser = new JwtCommonUser();
        commonUser.setOpenid("terry001");
        commonUser.setUserId(9999L);
//        if(user == null || !user.getPassword().equals(new Sha256Hash(form.getPassword(), user.getSalt()).toHex())) {
//            return GenericResponse.response(ServiceError.LOGIN_ERROR_USERNAME);
//
//        }
        String token = JwtTokenUtil.generateToken(commonUser);
        OAuth2Token tk = new OAuth2Token(token);
        SecurityUtils.getSubject().login(tk);
        return token;
    }
    @GetMapping("/test/base/stock")
    public String testBaseStock() throws InterruptedException {
        fetchDataService.initBaseDayKpi2("2024-11-01","2024-11-08");
        return "success";
    }

    @GetMapping("/test/print")
    public String print()  {
        log.info("测试打印");
        return "print";
    }

    @GetMapping("/test/base/history")
    public String testBaseHisotry() throws InterruptedException {
        for (int i = 0; i < 20; i++) {
            fetchDataService.sendBaseHistoryTask("2024-11-01","2024-11-10");
        }
        return "success";
    }

    //每日收盘后统计当天实时数据
    @GetMapping("/test/staCurrentDelay")
    public String staCurrentDelay(@RequestParam("date") String date,
                                  @RequestParam(value = "stockCode",required = false) String stockCode ) throws InterruptedException {
        fetchDataService.staCurrentDelay(date,stockCode);
        return "success";
    }


    /**
     * 初始化板块
     * @return
     * @throws InterruptedException
     */
    @GetMapping("/test/init/stockplates")
    public String initStockPlate() throws InterruptedException {
        stockPlateService.init();
        return "success";
    }

    /**
     * 股票和板块数据
     * @return
     * @throws InterruptedException
     */
    @GetMapping("/test/handlePlateStocks")
    public String handlePlateStocks() throws InterruptedException {
        fetchDataService.handlePlateStocks();
        return "success";
    }

    @GetMapping("/test/staKpiDelay")
    public String staKpiDelay(@RequestParam(value = "date",required = false) String date,
                              @RequestParam(value = "stockCode",required = false) String stockCode) throws InterruptedException {
        fetchDataService.staKpiDelay(date,stockCode );
        return "success";
    }

    /**
     * 初始化所有股票
     * @return
     * @throws InterruptedException
     */
    @GetMapping("/test/init/stocks")
    public String initStocks() throws InterruptedException {
        fetchDataService.maintainMonthly();
        return "success";
    }

    /**
     * 补历史kpi 直接调取网站的kpi指标
     * @param startDate
     * @param endDate
     * @param stockCode
     * @return
     * @throws InterruptedException
     */
    @GetMapping("/test/historyKpi")
    public String historyKPI(@RequestParam(value = "startDate",required = false) String startDate,
                             @RequestParam(value = "endDate",required = false) String endDate,
                             @RequestParam(value = "stockCode",required = false) String stockCode) throws InterruptedException {
        fetchDataService.sendBaseHistoryOneByOne(startDate,endDate,stockCode);
        return "success";
    }


    @GetMapping("/test/avg")
    public String setAvg(@RequestParam(value = "startDate",required = false) String startDate,
                             @RequestParam(value = "endDate",required = false) String endDate,
                             @RequestParam(value = "stockCode",required = false) String stockCode) throws InterruptedException {

        List<StockAll> list =  stockCommonQueryApi.getAllStocks().stream().map(r -> new StockAll(null,r.getStockCode(),r.getGl(),r.getName(),r.getJys())).collect(Collectors.toList());
//        fetchDataService.setAvg(list.subList(0,1),startDate,endDate);
        fetchDataService.setAvg(list,startDate,endDate);
        return "success";
    }





}
